import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

import javax.swing.JPanel;


public class EditPanel extends JPanel implements MouseListener, MouseMotionListener{
	
	ArrayList<Planet> planets = new ArrayList<Planet>();
	Planet selectedPlanet = null;
	Planet editPlanet = null;
	public EditPanel(){
		setBackground(Color.white);
		setPreferredSize(new Dimension(640, 480));
		addMouseListener(this);
		addMouseMotionListener(this);
	}
	@Override
	public void paint(Graphics g){
		super.paint(g);
		Graphics2D g2 = (Graphics2D)g;
		
		for (int i = 0; i < planets.size(); i++)
			planets.get(i).paint(g2);
		
		if (editPlanet != null)
			editPlanet.paint(g2);
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		for (int i = 0; i < planets.size();i++) {
			int dx = planets.get(i).getX() - e.getX();
			int dy = planets.get(i).getY() - e.getY();
			if (Math.sqrt(dx*dx+dy*dy)< planets.get(i).getRadius()) {
				selectedPlanet = planets.get(i);
				break;
			}
		}
		if (selectedPlanet == null && e.getButton() == MouseEvent.BUTTON1){
			editPlanet = new Planet();
			editPlanet.setX(e.getX());
			editPlanet.setY(e.getY());
		}
		else {
			if (e.getButton() == MouseEvent.BUTTON2) {
				Planet copyPlanet = new Planet();
				copyPlanet.setRadius(selectedPlanet.getRadius());
				copyPlanet.setNumber(selectedPlanet.getNumber());
				copyPlanet.setX(selectedPlanet.getX());
				copyPlanet.setY(selectedPlanet.getY());
				
				planets.add(copyPlanet);
				selectedPlanet = copyPlanet;
			}
		}
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		selectedPlanet = null;
		if (e.getButton() == MouseEvent.BUTTON1){
			if (editPlanet != null) {
				if (editPlanet.getRadius() < 15)
					editPlanet.setRadius(15);
				planets.add(editPlanet);
				editPlanet = null;
			}
		}
		else if (e.getButton() == MouseEvent.BUTTON3) {
			int removeIndex = -1;
			for (int i = 0; i < planets.size();i++) {
				int dx = planets.get(i).getX() - e.getX();
				int dy = planets.get(i).getY() - e.getY();
				if (Math.sqrt(dx*dx+dy*dy)< planets.get(i).getRadius()) {
					removeIndex = i;
					break;
				}
			}
			if (removeIndex != -1)
				planets.remove(removeIndex);
		}
	}
	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub

		if (editPlanet != null) {
			int dx = editPlanet.getX() - e.getX();
			int dy = editPlanet.getY() - e.getY();
			editPlanet.setRadius((int)Math.sqrt(dx*dx+dy*dy));
		}
		if (selectedPlanet != null) {
			selectedPlanet.setX(e.getX());
			selectedPlanet.setY(e.getY());
		}

	}
	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
}
